package com.towker.frame.controller.sys;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.log.BaseException;
import com.towker.core.util.BeanUtils;
import com.towker.frame.model.rbac.Subsystem;
import com.towker.frame.model.sys.User;
import com.towker.frame.service.MainService;
import com.towker.frame.service.sys.log.TSysLogService;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;


@Controller
@RequestMapping("/manage")
public class MainController
{
	private MainService service;
	
	//注入主业务对象。
	public void setMainService(MainService service)
	{
		this.service = service;
	}
	
	/**
	 * 到默认的主页视图。
	 * @throws IOException
	 */
	@RequestMapping(value = "/main")
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ModelAndView result = new ModelAndView();
		MyRequest req=new MyRequest(request);
		long sysid=req.getLong("sysid");
		if(sysid==0)
		{
			sysid= UserShare.getCurrentSystemId(request);
		}
		if(sysid==0){
			String ctx=request.getContextPath();
			response.sendRedirect(ctx + "/commons/noRights.jsp");
			return null;
		}
		Subsystem sys = service.getSysById(sysid);
		User user=UserShare.getCurrentUser(request);
		//sys,user.
		result.addObject("sys", sys);
		result.addObject("user",user);
		{
			result.setViewName("/manage/main.jsp");
			UserShare.setCurrentSystemId(request, response, sysid+"");
			UserShare.clearCache(request);
		}
//		result.setViewName("/manage/main.jsp");
//		UserShare.setCurrentSystemId(request, response, sysid+"");
//		UserShare.clearCache(request);
		return result;
	}
}
